class Strip < ActiveRecord::Base
  belongs_to :webcomic
  has_many :comments

  has_attachment :content_type => :image,
                 :storage => :file_system

  validates_as_attachment
  validates_presence_of :title, :filename
  validates_associated :webcomic


  def self.getMostRecentStrip(p_webcomic_id)
    allStrips = Strip.find(:all, :conditions => { :webcomic_id => p_webcomic_id})
    if !allStrips || allStrips.length == 0
      return nil
    else
      maxValue = -1
      mostRecent = nil
      allStrips.each do |i|
        if i.strip_number > maxValue
          maxValue = i.strip_number
          mostRecent = i
        end
      end
      return mostRecent
    end
  end

  def self.getNextStripNumber(p_webcomic_id)
    mostRecent = Strip.getMostRecentStrip(p_webcomic_id)
    latestNumber = 0
    if mostRecent
      latestNumber = mostRecent.strip_number
    end
    latestNumber = latestNumber + 1
  end
  
end
